
#include <iostream>
#include <memory>
#include <string>

// Step 1: 产品类 - 定义要构建的复杂对象，即电脑
class Computer {
public:
    void setCPU(const std::string& cpu) { CPU = cpu; }
    void setGPU(const std::string& gpu) { GPU = gpu; }
    void setMemory(const std::string& memory) { Memory = memory; }
    void setStorage(const std::string& storage) { Storage = storage; }
    void setOS(const std::string& os) { OS = os; }

    void showSpecifications() const {
        std::cout << "Computer Specifications:\n";
        std::cout << "CPU: " << CPU << "\n";
        std::cout << "GPU: " << GPU << "\n";
        std::cout << "Memory: " << Memory << "\n";
        std::cout << "Storage: " << Storage << "\n";
        std::cout << "Operating System: " << OS << "\n";
    }

private:
    std::string CPU, GPU, Memory, Storage, OS;
};

// Step 2: 抽象建造者接口 - 定义创建产品各部分的接口
class ComputerBuilder {
public:
    virtual ~ComputerBuilder() = default;
    virtual void buildCPU() = 0;
    virtual void buildGPU() = 0;
    virtual void buildMemory() = 0;
    virtual void buildStorage() = 0;
    virtual void installOS() = 0;
    virtual std::unique_ptr<Computer> getComputer() = 0;
};

// Step 3: 具体建造者类 - 具体实现建造过程
class GamingComputerBuilder : public ComputerBuilder {
public:
    GamingComputerBuilder() { computer = std::make_unique<Computer>(); }

    void buildCPU() override { computer->setCPU("Intel i9 11900K"); }
    void buildGPU() override { computer->setGPU("NVIDIA RTX 3080"); }
    void buildMemory() override { computer->setMemory("32GB DDR4"); }
    void buildStorage() override { computer->setStorage("1TB NVMe SSD"); }
    void installOS() override { computer->setOS("Windows 11"); }

    std::unique_ptr<Computer> getComputer() override { return std::move(computer); }

private:
    std::unique_ptr<Computer> computer;
};

class OfficeComputerBuilder : public ComputerBuilder {
public:
    OfficeComputerBuilder() { computer = std::make_unique<Computer>(); }

    void buildCPU() override { computer->setCPU("Intel i5 11400"); }
    void buildGPU() override { computer->setGPU("Integrated Graphics"); }
    void buildMemory() override { computer->setMemory("16GB DDR4"); }
    void buildStorage() override { computer->setStorage("512GB SSD"); }
    void installOS() override { computer->setOS("Windows 10"); }

    std::unique_ptr<Computer> getComputer() override { return std::move(computer); }

private:
    std::unique_ptr<Computer> computer;
};

// Step 4: 指挥者类 - 定义构建的步骤顺序，但不会涉及具体的构建细节
class Director {
public:
    void setBuilder(ComputerBuilder* builder) { this->builder = builder; }

    void constructComputer() {
        builder->buildCPU();
        builder->buildGPU();
        builder->buildMemory();
        builder->buildStorage();
        builder->installOS();
    }

private:
    ComputerBuilder* builder;
};

// 客户端代码
int main() {
    Director director;

    GamingComputerBuilder gamingBuilder;
    OfficeComputerBuilder officeBuilder;

    // 构建游戏电脑
    director.setBuilder(&gamingBuilder);
    director.constructComputer();
    auto gamingComputer = gamingBuilder.getComputer();
    std::cout << "Gaming Computer:\n";
    gamingComputer->showSpecifications();

    // 构建办公电脑
    director.setBuilder(&officeBuilder);
    director.constructComputer();
    auto officeComputer = officeBuilder.getComputer();
    std::cout << "\nOffice Computer:\n";
    officeComputer->showSpecifications();

    return 0;
}
